<template>
  <el-dialog :visible.sync="dialogVisible" :title="curId ? '编辑部门' : '新增部门'" width="700px" :before-close="close">
    <el-form ref="ruleForm" label-position="left  " :model="ruleForm" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="部门" prop="name">
        <el-input v-model="ruleForm.name" clearable />
      </el-form-item>
      <el-form-item label="父部门" prop="pid">
        <treeselect
          v-model="ruleForm.pid"
          :options="departments"
          style="width: 178px"
          placeholder="选择父部门"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
  import { getDepartment, updateDepartment, createDepartment } from '@/api/system/departments'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  export default {
    name: 'CuForm',
    components: { Treeselect },
    props: {
      dialogVisible: {
        type: Boolean,
        default: false
      },
      curId: {
        type: Number,
        default: null
      },
      departments: {
        type: Array,
        default: () => {
          return
        }
      }
    },

    data() {
      return {
        ruleForm: {
          name: '',
          pid: null
        },
        rules: {
          name: [{ required: true, trigger: 'blur', message: '部门名不能为空' }]
        }
      }
    },
    watch: {
      dialogVisible(v) {
        if (v) {
          console.log(v);
          if (this.curId) {
            getDepartment(this.curId).then(res => {
              this.ruleForm = res.data
            })
          }
        }
      }
    },
    methods: {
      close() {
        this.$refs.ruleForm.resetFields()
        this.$emit('close')
      },
      search() {
        this.close()
        this.$emit('search')
      },
      // 提交表单
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.curId) {
              if (!this.ruleForm.pid) {
                this.ruleForm.pid = null
              }
              updateDepartment(this.curId, this.ruleForm).then(res => {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
                this.search()
              })
            } else {
              createDepartment(this.ruleForm).then(res => {
                this.$message({
                  message: '新增成功',
                  type: 'success'
                })
                this.search()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      }
    }
  }
</script>
